#include "file_manager.h"

// 列出目录内容
int list_directory(const char *path) {
    DIR *dir;
    struct dirent *entry;
    struct stat stat_buf;
    char full_path[1024];
    
    dir = opendir(path);
    if (dir == NULL) {
        printf("无法打开目录: %s\n", strerror(errno));
        return -1;
    }
    
    printf("目录内容: %s\n", path);
    printf("--------------------------------\n");
    
    while ((entry = readdir(dir)) != NULL) {
        // 跳过 . 和 ..
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
            continue;
        }
        
        // 构建完整路径
        snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
        
        // 获取文件信息
        if (stat(full_path, &stat_buf) == 0) {
            printf("%s  ", entry->d_name);
            printf(S_ISDIR(stat_buf.st_mode) ? "[目录]" : "[文件]");
            printf("  大小: %ld 字节\n", stat_buf.st_size);
        }
    }
    
    printf("--------------------------------\n");
    closedir(dir);
    return 0;
}

// 创建目录
int create_directory(const char *path) {
    if (mkdir(path, 0777) == 0) {
        printf("目录创建成功: %s\n", path);
        return 0;
    } else {
        printf("目录创建失败: %s\n", strerror(errno));
        return -1;
    }
}